package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.dao.PermissionDao;
import com.ghh.dao.RoleDao;
import com.ghh.dao.UserDao;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.pojo.Permission;
import com.ghh.pojo.Role;
import com.ghh.pojo.User;
import com.ghh.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        //根据用户名查询用户基本信息
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }
        //获取用户的id
        Integer userId = user.getId();
        //根据id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        //遍历角色集合
        for (Role role : roles) {
            //根据id查询对应的权限
            Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
            //将角色与权限关联
            role.setPermissions(permissions);
        }
        //将用户与角色关联
        user.setRoles(roles);
        return user;
    }

    //查询用户管理分页操作，并条件查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page = userDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //查询所有角色
    public List<Role> findAll() {
        return userDao.findAll();
    }

    //添加用户
    public void add(Integer[] roleIds, User user) {
        userDao.add(user);

        Integer userId = user.getId();
        if (roleIds != null && roleIds.length > 0){
            for (Integer integer : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",integer);
                map.put("user_id",userId);
                userDao.setUserAndRole(map);
            }
        }
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    //根据用户ID 查询 关联的角色id
    public List<Integer> findRoleIdsByUserId(Integer id) {
        System.out.println("id = " + id);
        return userDao.findRoleIdsByUserId(id);
    }

    //修改信息
    public void edit(Integer[] roleIds, User user) {
        userDao.edit(user);
        userDao.deleteUserAndRoleById(user.getId());
        if (roleIds != null && roleIds.length > 0){
            for (Integer integer : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",integer);
                map.put("user_id",user.getId());
                userDao.setUserAndRole(map);
            }
        }

    }

    //删除用户
    public void delete(Integer id) {
        userDao.deleteUserAndRoleById(id);
        userDao.deleteUserById(id);
    }
}
